#include "dbapp.h"
#include <QCoreApplication>
#include <QDebug>
#include "spdlog/spdlog.h"
#include "mylogger.h"
#include <QSqlQuery>
#include <QSqlError>

using namespace ns_yht;

DbApp *DbApp::g_dbApp = nullptr;

DbApp::DbApp() : m_dbType(DBType::DTSqllite)
{
    if (QSqlDatabase::contains("qt_sql_app_connection"))
        m_dbApp = QSqlDatabase::database("qt_sql_app_connection");
    else
        m_dbApp = QSqlDatabase::addDatabase("QSQLITE", "qt_sql_app_connection");

    m_dbApp.setDatabaseName(QCoreApplication::applicationDirPath() + "/yhtapp.db");

    if (m_dbApp.open())
    {
        if (GLOG)
            GLOG->info("dbApp open sqlite ok!");
    }
    else
    {
        if (GLOG)
            GLOG->info("dbApp open sqlite error!");
    }

    if (!g_dbApp)
        g_dbApp = this;
}

DbApp::DbApp(const QString &host, const QString &database, const QString &uid, const QString &pwd)
    : m_dbType(DBType::DTSqlserver)
{
    if (QSqlDatabase::contains("qt_sql_app_connection"))
    {
        m_dbApp = QSqlDatabase::database("qt_sql_app_connection");
    }
    else
    {
        m_dbApp = QSqlDatabase::addDatabase("QODBC", "qt_sql_app_connection");
    }
    m_dbApp.setDatabaseName(QString("DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;UID=%3;PWD=%4;")
                                .arg(host, database, uid, pwd));
    if (m_dbApp.open())
        qInfo() << "dbApp open sqlserver ok!";
    else
        qInfo() << "dbApp open sqlserver error!" << host << database;
    g_dbApp = this;
}

DbApp::~DbApp()
{
    if (m_dbApp.isOpen())
        m_dbApp.close();
    g_dbApp = nullptr;
    if (GLOG)
        GLOG->info("DbApp close sqlite ok!");
}

void DbApp::reloadDB()
{
    if (m_dbApp.isOpen())
        m_dbApp.close();
    if (QSqlDatabase::contains("qt_sql_app_connection"))
        m_dbApp = QSqlDatabase::database("qt_sql_app_connection");
    else
        m_dbApp = QSqlDatabase::addDatabase("QSQLITE", "qt_sql_app_connection");

    m_dbApp.setDatabaseName(QCoreApplication::applicationDirPath() + "/yhtapp.db");

    m_dbApp.open();
}
// void DbApp::connect(const QString &host, const QString &database, const QString &uid, const QString &pwd)
// {
//     if (m_dbApp.isOpen())
//     {
//         m_dbApp.close();
//     }
//     else
//     {
//         m_dbApp.setDatabaseName(QString("DRIVER={SQL SERVER};SERVER=%1;DATABASE=%2;UID=%3;PWD=%4;")
//                                     .arg(host, database, uid, pwd));
//     }
//     if (m_dbApp.open())
//         qInfo() << "dbApp reopen sqlserver ok!";
//     else
//         qInfo() << "dbApp reopen sqlserver error!" << host << database;
// }

DbApp *DbApp::instance()
{
    return g_dbApp;
}

QSqlDatabase &DbApp::getDbApp()
{
    return m_dbApp;
}

bool DbApp::isOk() const
{
    return m_dbApp.isOpen();
}

DBType DbApp::getDbType() const
{
    return m_dbType;
}

void DbApp::showErrorInfo(const QString &module, const QSqlQuery &query)
{
    if (GLOG)
        GLOG->info("{}.lastError()----------------------{}", module.toStdString(), query.lastError().text().toStdString());
}
